package com.xp.basic;


import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Set;

/**
 * nio 初识 nio 一个基础流程
 */
public class Test1 {

    /*public static void main(String[] args) throws IOException {
        //1.打开ServerSocketChannel 用于监听客户端的链接
        ServerSocketChannel open = ServerSocketChannel.open();
        //2.绑定监听端口，设置连接为非阻塞模式
        open.socket().bind(new InetSocketAddress(InetAddress.getByName("localhost"),8080));
        //  设置为非阻塞模式
        open.configureBlocking(false);

        //3.创建反应式线程，创建多路复用器并启动线程
        Selector selector = Selector.open();
//        new Thread(new ReactorTask()).start();
        //4.将serverSocketChannel 注册到Reactor线程的多路复用器 selector 上，监听ACCEPT 事件
        open.register(selector, SelectionKey.OP_ACCEPT,new Object());

        //5.多路复用器在线程run方法的无限循环体内轮询准备就绪的key，代码如下
        int select = selector.select();
        Set<SelectionKey> selectionKeys = selector.selectedKeys();
        Iterator<SelectionKey> it = selectionKeys.iterator();
        while (it.hasNext()) {
            SelectionKey next = it.next();
            // do something
        }

        //6.多路复用器监听到有新的客户端接入，处理新的接入请求，王城TCP三次握手，建立物理链路
        SocketChannel channel = open.accept();
        //7.设置客户端的链路为非阻塞模式
        channel.configureBlocking(false);
        channel.socket().setReuseAddress(true);

        //8.将新接入的客户端链接注册到Reactor 线程的多路复用器上，监听读操作
        channel.register(selector,SelectionKey.OP_READ,new Object());

        //9.异步读取客户端的请求消息到缓冲区
        ByteBuffer byteBuffer = null;
        int readNum = channel.read(byteBuffer);

        //10.对ByteBuffer 进行编解码，如果有半包消息指针，reset 继续读取后续的报文，
        //   将解码成功的消息封装成task，投递到业务线程中，进行业务逻辑编排
        Object msg = null;
        while (byteBuffer.hasRemaining()){
            byteBuffer.mark();
            //解码
            // msg = decode(byteBuffer);
            if(msg == null){
                byteBuffer.reset();
                break;
            }

        }
    }*/

    public static void main(String[] args) {

        testByteBufferMethod();
    }

    public static  void testByteBufferMethod(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        System.out.println("byteBuffer.position() = " + byteBuffer.position());
        System.out.println("byteBuffer.limit() = " + byteBuffer.limit());
        System.out.println("byteBuffer.capacity() = " + byteBuffer.capacity());
        byteBuffer.putChar('a');
        System.out.println("------------------put a ---------------------");
        System.out.println("byteBuffer.position() = " + byteBuffer.position());
        System.out.println("byteBuffer.limit() = " + byteBuffer.limit());
        System.out.println("byteBuffer.capacity() = " + byteBuffer.capacity());
        byteBuffer.flip();
        System.out.println("------------------flip---------------------");
        System.out.println("byteBuffer.position() = " + byteBuffer.position());
        System.out.println("byteBuffer.limit() = " + byteBuffer.limit());
        System.out.println("byteBuffer.capacity() = " + byteBuffer.capacity());
        char aChar = byteBuffer.getChar(0);
        System.out.println("------------------getChar :"+ aChar +"---------------------");
        System.out.println("byteBuffer.position() = " + byteBuffer.position());
        System.out.println("byteBuffer.limit() = " + byteBuffer.limit());
        System.out.println("byteBuffer.capacity() = " + byteBuffer.capacity());


        ByteBuffer hello = StandardCharsets.UTF_8.encode("hello");
        System.out.println("hello.limit() = " + hello.limit());
        System.out.println("hello.position() = " + hello.position());
        System.out.println("hello.capacity() = " + hello.capacity());
        System.out.println("hello.remaining() = " + hello.remaining());
        System.out.println("hello.get(1) = " + hello.get(0));
        System.out.println("hello.get(1) = " + hello.get(1));

        System.out.println("hello.position() = " + hello.position());
        System.out.println("hello.limit() = " + hello.limit());
        System.out.println("hello.capacity() = " + hello.capacity());
        System.out.println("hello.remaining() = " + hello.remaining());

    }
}
